package com.salesmanager.core.business.report.service;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.salesmanager.core.business.search.service.SearchServiceImpl;

import net.sf.jasperreports.engine.JRAbstractExporter;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import net.sf.jasperreports.export.SimpleHtmlExporterConfiguration;
import net.sf.jasperreports.export.XlsxExporterConfiguration;

import com.mchange.v2.c3p0.ComboPooledDataSource;

@Service("jasperReportService")
public class JasperReportServiceImpl implements JasperReportService {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(JasperReportServiceImpl.class);
	private static SimpleDateFormat dateFormat = new SimpleDateFormat(
			"ddMMyyyy-hhmmss");

	@Autowired
	private DataSource dataSource;

	public JasperReportServiceImpl(){
		
	}
	
	@Override
	public JasperReport compileReport(InputStream inputFile) {
		try {
			return JasperCompileManager.compileReport(inputFile);
		} catch (Exception e) {
			LOGGER.error("Error while compile report ", e);
		} 
		return null;
	}

	@Override
	public JasperPrint fillReport(JasperReport report, Map parameters) {
		try {
			Connection conn = dataSource.getConnection();
			return JasperFillManager.fillReport(report, parameters, conn);
		} catch (Exception e) {
			LOGGER.error("Error while fill report ", e);
		}
		return null;
	}
	
	@Override
	public JasperPrint fillReport(InputStream input, Map parameters) {
		try {
			Connection conn = dataSource.getConnection();
			return JasperFillManager.fillReport(input, parameters, conn);
		} catch (Exception e) {
			LOGGER.error("Error while fill report ", e);
		}
		return null;
	}

	@Override
	public byte[] exportToExcel(JasperPrint filledReport) {

        ByteArrayOutputStream byteArrayOutputStream;
        byteArrayOutputStream = new ByteArrayOutputStream();
        
		String outputName = filledReport.getName() + "_"
				+ dateFormat.format(System.currentTimeMillis()) + ".xls";

		JRAbstractExporter exporter = new JRXlsxExporter();
		exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outputName);
		exporter.setParameter(JRExporterParameter.JASPER_PRINT, filledReport);
		exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, byteArrayOutputStream);
		
		try {
			exporter.exportReport();
		} catch (JRException e) {
			LOGGER.error("Error while export to Excel report ", e);
		}
		return byteArrayOutputStream.toByteArray();
	}

	@SuppressWarnings("deprecation")
	@Override
	public byte[] exportToPdf(JasperPrint filledReport) {
        ByteArrayOutputStream byteArrayOutputStream;
        byteArrayOutputStream = new ByteArrayOutputStream();
        
		String outputName = filledReport.getName() + "_"
				+ dateFormat.format(System.currentTimeMillis()) + ".pdf";

		JRAbstractExporter exporter = new JRPdfExporter();

		// Configure the exporter (set output file name and print object)
		exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outputName);
		exporter.setParameter(JRExporterParameter.JASPER_PRINT, filledReport);
		exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, byteArrayOutputStream);
		exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
		try {
			exporter.exportReport();
		} catch (JRException e) {
			LOGGER.error("Error while export to Excel report ", e);
		}
		return byteArrayOutputStream.toByteArray();
	}

	@Override
	public byte[] exportToHtml(JasperPrint filledReport) {
        ByteArrayOutputStream byteArrayOutputStream;
        byteArrayOutputStream = new ByteArrayOutputStream();
        
        JRHtmlExporter exporter = new JRHtmlExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, filledReport);
		exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, byteArrayOutputStream);
		exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
				
		try {
			exporter.exportReport();
		} catch (JRException e) {
			LOGGER.error("Error while export to Excel report ", e);
		}
		return byteArrayOutputStream.toByteArray();
	}
	
	@Override
	public byte[] exportToHtml(JasperPrint filledReport, int page) {
        ByteArrayOutputStream byteArrayOutputStream;
        byteArrayOutputStream = new ByteArrayOutputStream();
        
        JRHtmlExporter exporter = new JRHtmlExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, filledReport);
		exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, byteArrayOutputStream);
		exporter.setParameter(JRExporterParameter.PAGE_INDEX, new Integer(page));
		exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
		
		try {
			exporter.exportReport();
		} catch (JRException e) {
			LOGGER.error("Error while export to Excel report ", e);
		}
		return byteArrayOutputStream.toByteArray();
	}
}
